let app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        fileList: [],
        qrcodeUrl: '',
        id: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.queryData()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    queryData: function () {
        let _this = this
        app.formGet('/app/brand/payment/info', null).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                if (res.data != null) {
                    let imageObj = {}
                    let tempFileList = []
                    let qrcodeUrl = res.data.qrcodeUrl
                    imageObj.url = qrcodeUrl
                    imageObj.isImage = true
                    tempFileList.push(imageObj)
                    _this.setData({
                        id: res.data.id,
                        qrcodeUrl: qrcodeUrl,
                        fileList: tempFileList
                    });
                }
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    handleSavePayment: function () {
        let _this = this
        _this.setData({
            btnDisable: true
        })
        let data = {}
        data.id = this.data.id
        data.qrcodeUrl = this.data.qrcodeUrl
        app.formPost('/app/brand/payment/add', data).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                app.message(res.message, 'success')
                setTimeout(function () {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 2000)
            } else {
                app.message(res.message, 'error')
            }
            _this.setData({
                btnDisable: false
            })
        }).catch(e => {
            _this.setData({
                spinShow: false,
                btnDisable: false
            });
            app.message(e, 'error')
        })
    },
    picAfterRead(event) {
        const {
            file
        } = event.detail;
        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        let _this = this
        wx.uploadFile({
            url: app.globalData.baseAPI + '/app/upload/pic', // 仅为示例，非真实的接口地址
            filePath: file.url,
            name: 'file',
            formData: {},
            header: {
                'Authorization': wx.getStorageSync('token')
            },
            success(res) {
                // 上传完成需要更新 fileList
                let result = JSON.parse(res.data)
                if (result.code === 0) {
                    let imageObj = {}
                    let tempFileList = []
                    imageObj.url = result.data
                    imageObj.isImage = true
                    tempFileList.push(imageObj)
                    _this.setData({
                        fileList: tempFileList,
                        qrcodeUrl: result.data
                    })
                }
            },
        });
    },
    deleteFile: function (e) {
        let index = e.detail.index
        let fileList = this.data.fileList
        fileList.splice(index, 1)
        this.setData({
            fileList: fileList
        })
    },
})